package com.qiyi.player.wonder.plugins.scenetile.view.greentailpart
{
   import flash.display.Sprite;
   import scenetile.GreenTailSoundMC;
   import scenetile.GreenTailBitmap;
   import flash.text.TextField;
   import common.CommonGreenBtn;
   import scenetile.GreenTailSendingMC;
   import com.qiyi.player.wonder.common.ui.SelectTextField;
   import flash.utils.ByteArray;
   import flash.display.Loader;
   import com.qiyi.player.base.logging.ILogger;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import com.qiyi.player.wonder.plugins.scenetile.SceneTileDef;
   import flash.text.TextFieldAutoSize;
   import flash.events.MouseEvent;
   import flash.events.TextEvent;
   import gs.TweenLite;
   import com.qiyi.player.wonder.plugins.scenetile.view.SceneTileEvent;
   import com.qiyi.player.wonder.common.utils.StringUtils;
   import flash.net.URLLoader;
   import flash.net.URLLoaderDataFormat;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import com.qiyi.player.wonder.common.config.SystemConfig;
   import flash.net.URLRequest;
   import com.adobe.serialization.json.JSON;
   import flash.utils.Endian;
   import com.qiyi.player.base.logging.Log;
   
   public class SoundVersionPanel extends Sprite
   {
      
      public function SoundVersionPanel() {
         this._log = Log.getLogger("com.qiyi.player.wonder.plugins.scenetile.view.greentailpart.SoundVersionPanel");
         super();
         this.initSoundPanel();
      }
      
      private static const REQUEST_TIMES:uint = 5;
      
      private static const DEF_TITLE_STRING:String = "绿尾巴咻咻，精彩随心看";
      
      private static const DEF_WARN_REST:String = "人太多了,休息,休息一下...";
      
      private static const DEF_WARN_ERROR:String = "请用图中建议的方法哦";
      
      private static const DEF_PAIRING:String = "正在努力搜索设备，请稍候...";
      
      private static const DEF_CUT_CONNECT:String = "连接中断，重新试试吧";
      
      private static const TEXT_CLIP_DESCRIBE_1:String = "1.打开爱奇艺视频APP\n   没安装   <a href=\'event:onTextEventClick\'>" + "<font color=\'#99cc33\'><u>马上下载</u></font></a>";
      
      private static const TEXT_CLIP_DESCRIBE_2:String = "2.点击App播放器中\n   右上角的绿尾巴";
      
      private static const TEXT_CLIP_DESCRIBE_3:String = "3.立即点击发送，精彩视频随处看";
      
      private var _greenTailMC:GreenTailSoundMC;
      
      private var _greenTailBitmap:GreenTailBitmap;
      
      private var _tfGreenTailHint:TextField;
      
      private var _sendBtn:CommonGreenBtn;
      
      private var _sendTf:TextField;
      
      private var _sendingMC:GreenTailSendingMC;
      
      private var _sendingTf:TextField;
      
      private var _againCollideBtn:CommonGreenBtn;
      
      private var _againCollideTf:TextField;
      
      private var _instantPassonBtn:CommonGreenBtn;
      
      private var _instantPassonTf:TextField;
      
      private var _cutConnectBtn:SelectTextField;
      
      private var _titleTF:TextField;
      
      private var _clipDescribeTF1:TextField;
      
      private var _clipDescribeTF2:TextField;
      
      private var _clipDescribeTF3:TextField;
      
      private var _uuid:String;
      
      private var _pcVideoName:String;
      
      private var _phoneVideoName:String;
      
      private var _jsonData:Object;
      
      private var _soundByteArray:ByteArray;
      
      private var _loader:Loader;
      
      private var _currentMateTimeNum:uint = 0;
      
      private var _currentPairNum:uint = 0;
      
      private var _isPair:Boolean = false;
      
      private var _log:ILogger;
      
      private function initSoundPanel() : void {
         this._greenTailMC = new GreenTailSoundMC();
         this._titleTF = FastCreator.createLabel(DEF_TITLE_STRING,16777215,20);
         this._titleTF.y = 14;
         this._titleTF.x = (SceneTileDef.PANEL_WIDTH - this._titleTF.width) / 2;
         this._greenTailMC.addChild(this._titleTF);
         this._clipDescribeTF1 = FastCreator.createLabel(TEXT_CLIP_DESCRIBE_1,16777215);
         this._clipDescribeTF1.x = 85;
         this._clipDescribeTF1.y = 65;
         this._greenTailMC.addChild(this._clipDescribeTF1);
         this._clipDescribeTF2 = FastCreator.createLabel(TEXT_CLIP_DESCRIBE_2,16777215);
         this._clipDescribeTF2.x = 270;
         this._clipDescribeTF2.y = 65;
         this._greenTailMC.addChild(this._clipDescribeTF2);
         this._clipDescribeTF3 = FastCreator.createLabel(TEXT_CLIP_DESCRIBE_3,16777215);
         this._clipDescribeTF3.x = 85;
         this._clipDescribeTF3.y = 125;
         this._greenTailMC.addChild(this._clipDescribeTF3);
         addChild(this._greenTailMC);
         this._sendBtn = new CommonGreenBtn();
         this._sendBtn.x = 260;
         this._sendBtn.y = 150;
         addChild(this._sendBtn);
         this._sendTf = FastCreator.createLabel("发送",16777215,14);
         this._sendTf.x = this._sendBtn.x + (this._sendBtn.width - this._sendTf.width) / 2;
         this._sendTf.y = this._sendBtn.y + (this._sendBtn.height - this._sendTf.height) / 2;
         this._sendTf.mouseEnabled = this._sendTf.selectable = false;
         addChild(this._sendTf);
         this._sendingMC = new GreenTailSendingMC();
         this._sendingMC.x = 260;
         this._sendingMC.y = 155;
         addChild(this._sendingMC);
         this._sendingTf = FastCreator.createLabel("发送中",16777215,14);
         this._sendingTf.x = this._sendingMC.x - 5 + (this._sendingMC.width - this._sendingTf.width) / 2;
         this._sendingTf.y = this._sendingMC.y + (this._sendingMC.height - this._sendingTf.height) / 2;
         this._sendingTf.mouseEnabled = this._sendingTf.selectable = false;
         addChild(this._sendingTf);
         this._greenTailBitmap = new GreenTailBitmap();
         this._greenTailBitmap.x = SceneTileDef.PANEL_WIDTH / 2;
         this._greenTailBitmap.y = 44;
         addChild(this._greenTailBitmap);
         this._tfGreenTailHint = FastCreator.createLabel("",16777215,18,TextFieldAutoSize.CENTER);
         this._tfGreenTailHint.y = this._greenTailBitmap.y + 32;
         addChild(this._tfGreenTailHint);
         this._againCollideBtn = new CommonGreenBtn();
         this._againCollideBtn.x = SceneTileDef.PANEL_WIDTH * 0.75 - this._againCollideBtn.width * 0.5;
         addChild(this._againCollideBtn);
         this._againCollideTf = FastCreator.createLabel("重新发送",16777215,14);
         this._againCollideTf.x = this._againCollideBtn.x + (this._againCollideBtn.width - this._againCollideTf.width) / 2;
         this._againCollideTf.mouseEnabled = this._againCollideTf.selectable = false;
         addChild(this._againCollideTf);
         this._instantPassonBtn = new CommonGreenBtn();
         this._instantPassonBtn.x = SceneTileDef.PANEL_WIDTH * 0.75 - this._instantPassonBtn.width * 0.5;
         addChild(this._instantPassonBtn);
         this._instantPassonTf = FastCreator.createLabel("立即传输",16777215,14);
         this._instantPassonTf.x = this._instantPassonBtn.x + (this._instantPassonBtn.width - this._instantPassonTf.width) / 2;
         this._instantPassonTf.selectable = this._instantPassonTf.mouseEnabled = false;
         addChild(this._instantPassonTf);
         this._cutConnectBtn = new SelectTextField("断开连接");
         addChild(this._cutConnectBtn);
         this._againCollideBtn.y = this._instantPassonBtn.y = this._cutConnectBtn.y = 120;
         this._instantPassonTf.y = this._instantPassonBtn.y + (this._instantPassonBtn.height - this._instantPassonTf.height) / 2;
         this._againCollideTf.y = this._againCollideBtn.y + (this._againCollideBtn.height - this._againCollideTf.height) / 2;
         this._sendBtn.addEventListener(MouseEvent.CLICK,this.onSendBtnClick);
         this._againCollideBtn.addEventListener(MouseEvent.CLICK,this.onSendBtnClick);
         this._instantPassonBtn.addEventListener(MouseEvent.CLICK,this.onInstantPassonBtnClick);
         this._cutConnectBtn.addEventListener(MouseEvent.CLICK,this.onCutConnectBtnClick);
         this._clipDescribeTF1.addEventListener(TextEvent.LINK,this.onLinkTextClick);
      }
      
      public function openPanel() : void {
         this._currentMateTimeNum = 0;
         this._currentPairNum = 0;
         this._phoneVideoName = "";
         this.showDefaultPanel(DEF_TITLE_STRING,true);
      }
      
      public function closePanel() : void {
         this._currentMateTimeNum = this._currentPairNum = 10;
         TweenLite.killTweensOf(this.onClosePanel);
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         TweenLite.killTweensOf(this.requestMateInfo);
         this._isPair = false;
         this.hideChildren();
      }
      
      public function get isPair() : Boolean {
         return this._isPair;
      }
      
      private function onLinkTextClick(param1:TextEvent) : void {
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_LinkTextClick));
      }
      
      private function onInstantPassonBtnClick(param1:MouseEvent = null) : void {
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         this.showOutputSuccessPanel(this._pcVideoName);
      }
      
      private function onCutConnectBtnClick(param1:MouseEvent) : void {
         TweenLite.killTweensOf(this.onClosePanel);
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         this.showDefaultPanel(DEF_TITLE_STRING,true);
      }
      
      private function hideChildren() : void {
         this._instantPassonBtn.visible = this._againCollideBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = this._greenTailMC.visible = this._sendBtn.visible = this._sendTf.visible = this._sendingMC.visible = this._instantPassonTf.visible = this._againCollideTf.visible = this._sendingTf.visible = this._cutConnectBtn.visible = false;
      }
      
      private function showDefaultPanel(param1:String, param2:Boolean = false) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_INIT));
         this._greenTailMC.visible = true;
         if(param1 == DEF_PAIRING)
         {
            this._sendingTf.visible = this._sendingMC.visible = true;
         }
         else
         {
            this._sendBtn.visible = this._sendTf.visible = true;
         }
         this._titleTF.htmlText = param1;
         if(param2)
         {
            this._greenTailMC.play();
         }
      }
      
      private function showConnectErrorPanel() : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._againCollideBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = this._againCollideTf.visible = true;
         this._tfGreenTailHint.htmlText = DEF_CUT_CONNECT;
         this._tfGreenTailHint.x = (SceneTileDef.PANEL_WIDTH - this._tfGreenTailHint.width) / 2;
      }
      
      private function showConnectSuccessPanel(param1:String) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._cutConnectBtn.visible = this._instantPassonBtn.visible = this._instantPassonTf.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = true;
         this._cutConnectBtn.x = SceneTileDef.PANEL_WIDTH * 0.5 - this._cutConnectBtn.width * 0.5;
         var _loc2_:* = "您已和" + param1 + "的手机连接";
         this._tfGreenTailHint.htmlText = StringUtils.remainWord(_loc2_,25);
         this._tfGreenTailHint.x = (SceneTileDef.PANEL_WIDTH - this._tfGreenTailHint.width) / 2;
         TweenLite.delayedCall(5,this.onInstantPassonBtnClick);
      }
      
      private function showOutputSuccessPanel(param1:String) : void {
         this.hideChildren();
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_PanelHeightChange,SceneTileDef.PANEL_HEIGHT_NONE));
         this._cutConnectBtn.visible = this._tfGreenTailHint.visible = this._greenTailBitmap.visible = true;
         this._cutConnectBtn.x = SceneTileDef.PANEL_WIDTH * 0.75 - this._cutConnectBtn.width * 0.5;
         this._tfGreenTailHint.htmlText = StringUtils.remainWord("请观看 " + param1,18);
         this._tfGreenTailHint.x = (SceneTileDef.PANEL_WIDTH - this._tfGreenTailHint.width) / 2;
         TweenLite.delayedCall(3,this.onClosePanel);
      }
      
      private function onClosePanel() : void {
         TweenLite.killTweensOf(this.onClosePanel);
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_GreenTailClose));
      }
      
      private function onSendBtnClick(param1:MouseEvent) : void {
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_SendBtnClick));
      }
      
      public function requestBlankEvent(param1:String, param2:String, param3:int, param4:String, param5:String) : void {
         this._uuid = param1;
         this._pcVideoName = param5;
         TweenLite.killTweensOf(this.onInstantPassonBtnClick);
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         var _loc6_:URLLoader = new URLLoader();
         _loc6_.dataFormat = URLLoaderDataFormat.BINARY;
         _loc6_.addEventListener(Event.COMPLETE,this.onRequestBlankEventComplete);
         _loc6_.addEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         _loc6_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc7_:String = SystemConfig.SOUND_VERSION_REQUEST_URL + "?uuid=" + param1 + "&device_name=pc" + "&device_type=pc" + "&event_type=1" + "&longitude=&latitude=&velocity=&flash=1" + "&tvid=" + param2 + "&videoPlayTime=" + param3 + "&albumId=" + param4 + "&videoName=" + encodeURIComponent(this._pcVideoName) + "&tn=" + Math.random();
         _loc6_.load(new URLRequest(_loc7_));
         this.showDefaultPanel(DEF_PAIRING);
         TweenLite.delayedCall(3,this.showConnectErrorPanel);
         this._log.debug("request sound data url : " + _loc7_);
      }
      
      private function onRequestBlankEventComplete(param1:Event) : void {
         var obj:Object = null;
         var var_2:Event = param1;
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         var_2.target.removeEventListener(Event.COMPLETE,this.onRequestBlankEventComplete);
         var_2.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         var_2.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         this._soundByteArray = null;
         try
         {
            obj = com.adobe.serialization.json.JSON.decode(var_2.target.data);
            if(obj.code == "A00001")
            {
               this.showConnectErrorPanel();
            }
            this._log.debug("request sound data code " + obj.code);
         }
         catch(e:Error)
         {
            _soundByteArray = var_2.target.data as ByteArray;
            loadSoundDataFormByte();
            _log.debug("request sound data succssd : " + _soundByteArray.length);
         }
      }
      
      private function loadSoundDataFormByte() : void {
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         if(this._soundByteArray == null)
         {
            this.showConnectErrorPanel();
            return;
         }
         if(this._loader != null)
         {
            this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onByteArrayLoaded);
            this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onLoaderIoError);
            this._loader = null;
         }
         try
         {
            this._soundByteArray.endian = Endian.LITTLE_ENDIAN;
            this._loader = new Loader();
            this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onByteArrayLoaded);
            this._loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onLoaderIoError);
            this._loader.loadBytes(this._soundByteArray);
            TweenLite.delayedCall(3,this.showConnectErrorPanel);
         }
         catch(e:Event)
         {
            _log.debug("sound play error : " + e);
         }
      }
      
      private function onByteArrayLoaded(param1:Event) : void {
         this._log.debug("sound play success");
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         TweenLite.delayedCall(3,this.requestMateInfo);
      }
      
      private function onLoaderIoError(param1:IOErrorEvent) : void {
         this._log.debug("sound play error : " + param1);
         this.showConnectErrorPanel();
      }
      
      private function requestMateInfo() : void {
         TweenLite.killTweensOf(this.requestMateInfo);
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         var _loc1_:URLLoader = new URLLoader();
         _loc1_.addEventListener(Event.COMPLETE,this.onRequestMateInfoComplete);
         _loc1_.addEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         _loc1_.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc2_:String = SystemConfig.MATE_INFO_SOUND_VERSION_URL + "?uuid=" + this._uuid + "&device_name=pc" + "&device_type=pc" + "&event_type=1" + "&tn=" + Math.random();
         _loc1_.load(new URLRequest(_loc2_));
         this._currentMateTimeNum++;
         this._log.debug("request sound data mate request url: " + _loc2_);
         TweenLite.delayedCall(3,this.showConnectErrorPanel);
      }
      
      private function onRequestMateInfoComplete(param1:Event) : void {
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         param1.target.removeEventListener(Event.COMPLETE,this.onRequestMateInfoComplete);
         param1.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         param1.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
         var _loc2_:Object = com.adobe.serialization.json.JSON.decode(param1.target.data);
         this._log.debug("request sound data mate code : " + _loc2_.code + "  times : " + this._currentMateTimeNum);
         if(_loc2_.code == "A00000")
         {
            this._jsonData = _loc2_.data;
            if(_loc2_.data == null)
            {
               this._phoneVideoName = "";
               this.showConnectSuccessPanel("null");
            }
            else
            {
               this.showConnectSuccessPanel(_loc2_.data.device_name);
               this._phoneVideoName = _loc2_.data.videoName;
            }
            this._isPair = false;
         }
         else if(this._currentMateTimeNum < REQUEST_TIMES)
         {
            this.loadSoundDataFormByte();
         }
         else
         {
            this._currentPairNum++;
            this._currentMateTimeNum = 0;
            if(this._currentPairNum >= REQUEST_TIMES)
            {
               this.showDefaultPanel(DEF_WARN_REST);
            }
            else
            {
               this.showDefaultPanel(DEF_WARN_ERROR);
            }
            this._isPair = false;
         }
         
      }
      
      private function onUrlError(param1:Event) : void {
         this.showConnectErrorPanel();
         TweenLite.killTweensOf(this.showConnectErrorPanel);
         param1.target.removeEventListener(IOErrorEvent.IO_ERROR,this.onUrlError);
         param1.target.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onUrlError);
      }
   }
}
